package com.atguigu.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.atguigu.base.BaseController;
import com.atguigu.entity.HouseUser;
import com.atguigu.service.HouseUserService;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;

/**
 * @Author: 翟文海
 * @Date: 2022/10/30/030 10:13
 * @Version: 1.0
 * @Description:
 **/
@Controller
@RequestMapping("/houseUser")
public class HouseUserController extends BaseController {
    private static final String PAGE_CREATE = "houseUser/create";
    private static final String PAGE_SUCCESS = "common/successPage";
    private static final String PAGE_EDIT = "houseUser/edit";
    private static final String LIST_HOUSE = "redirect:/house";
    @Reference
    private HouseUserService houseUserService;

    @GetMapping("/create")
    public String create(HouseUser houseUser, Model model) {
        model.addAttribute("houseUser", houseUser);
        return toPage(PAGE_CREATE);
    }

    @PostMapping("/save")
    public String save(HouseUser houseUser, Model model) {
        houseUserService.insert(houseUser);
        return toPage(model, "添加房东信息成功！", PAGE_SUCCESS);
    }

    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") Long id, Model model) {
        HouseUser houseUser = houseUserService.getById(id);
        model.addAttribute("houseUser", houseUser);
        return toPage(PAGE_EDIT);
    }

    @PostMapping("/update")
    public String update(HouseUser houseUser, Model model) {
        houseUserService.update(houseUser);
        return toPage(model, "修改房东信息成功！", PAGE_SUCCESS);
    }

    @GetMapping("/delete/{houseId}/{id}")
    public String delete(@PathVariable("houseId") Long houseId ,@PathVariable("id") Long id, Model model) {
        houseUserService.delete(id);
        return toPage(LIST_HOUSE+"/"+houseId);
    }
}
